﻿using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Common;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.ComponentModel.DataAnnotations.Schema;
using mvcForum.Core;

namespace mvcForum.DataProvider.EntityFramework {

	public class MembershipDbContext : MVCForumContext {

		public DbSet<User> Users { get; set; }
		public DbSet<Role> Roles { get; set; }

		public MembershipDbContext() : base("DefaultConnection") { }
		public MembershipDbContext(String nameOrConnectionString) : base(nameOrConnectionString) { }

		protected override void OnModelCreating(DbModelBuilder modelBuilder) {
			base.OnModelCreating(modelBuilder);

			modelBuilder.Entity<User>().Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
			modelBuilder.Entity<Role>().Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
			
			modelBuilder.Entity<User>().ToTable("User");
			modelBuilder.Entity<User>().Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
			modelBuilder.Entity<User>().Property(u => u.Username).IsRequired().HasMaxLength(256);
			modelBuilder.Entity<User>().Property(u => u.EmailAddress).IsRequired().HasMaxLength(200);
			modelBuilder.Entity<User>().Property(u => u.Password).IsRequired().HasMaxLength(100);
			modelBuilder.Entity<User>().Property(u => u.Created).IsRequired();
			modelBuilder.Entity<User>().Property(u => u.LastVisit).IsRequired();
			modelBuilder.Entity<User>().Property(u => u.Locked).IsRequired();
			modelBuilder.Entity<User>().Property(u => u.Approved).IsRequired();
			modelBuilder.Entity<User>().Property(u => u.LastPasswordFailure).IsRequired();
			modelBuilder.Entity<User>().Property(u => u.PasswordFailures).IsRequired();
			modelBuilder.Entity<User>().Property(u => u.LastLockout).IsRequired();

			modelBuilder.Entity<Role>().ToTable("Role");
			modelBuilder.Entity<Role>().Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
			modelBuilder.Entity<Role>().Property(u => u.Name).IsRequired().HasMaxLength(256);

		}
	}
}